package com.translate.web.api.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.msg.ResponseCode;
import com.response.vo.ResponseModel;
import com.returns.value.ICodeAndData;
import com.translate.entity.IdAndRecommendName;
import com.translate.entity.User;
import com.jwt.JwtTokenUtil;
import com.translate.web.param.CheckUserExist;
import com.translate.web.param.LoginParam;
import com.translate.web.param.LoginParamBySms;
import com.translate.web.param.RegisterParamByMobile;
import com.translate.web.param.RetrievePasswordParamByEmail;
import com.translate.web.param.RetrievePasswordParamByPhone;
import com.translate.web.param.UserAddParam;
import com.translate.web.service.ICommissionManagementService;
import com.translate.web.service.IUserService;

@RestController
@RequestMapping("/user")
public class UserController {

	private IUserService userService;

	ICommissionManagementService commissionManagementService;

	private JwtTokenUtil jwtComponent;

	@Autowired
	public void setCommissionManagementService(ICommissionManagementService commissionManagementService) {
		this.commissionManagementService = commissionManagementService;
	}

	public UserController(IUserService userService) {
		this.userService = userService;
	}

	/**
	 * 账号密码登录
	 * 
	 * @return
	 */
	@PostMapping("/checkUserExist")
	public ResponseModel<Integer> checkUserExist(@RequestBody @Validated CheckUserExist userExist) {

		boolean res = this.userService.checkUserExits(userExist.getAccount());

		if (!res) {
			return ResponseModel.<Integer>errorMsg(0, ResponseCode.ERROR_NOT_FOUND_USER);
		}
		return ResponseModel.<Integer>success(1);
	}

	/**
	 * 邮箱密码登录
	 *
	 * @return
	 */
	@PostMapping("/loginByEmail")
	public ResponseModel<String> loginByEmail(@RequestBody @Validated LoginParam loginParam) {

		ICodeAndData<User> codeAndData = this.userService.loginByPwd(loginParam);

		ResponseCode responseCode = codeAndData.getResponseCode();

		if (0 != ResponseCode.SUCCESS.compareTo(responseCode)) {
			return ResponseModel.<String>errorMsg("", responseCode);
		}

		Map<String, Object> map = new HashMap<>();

		User user = codeAndData.getData();

		User update = new User();

		Integer loginNum = user.getLoginNum() == null ? 0 : user.getLoginNum();
		loginNum++;
		update.setLoginNum(loginNum);

		long currentTime = (System.currentTimeMillis() / 1000);

		Long id = user.getId();
		update.setId(id);

		update.setLastLoginTime(currentTime);

		// 更新用户最后登录时间和登录次数
		this.userService.updateByPrimaryKeySelective(update);
		map.put("id", id);
		map.put("userName", user.getUserName());
		map.put("realname", user.getRealname());
		map.put("mobile", user.getMobile());
		String token = jwtComponent.generateToken(map);

		return ResponseModel.<String>success(token);
	}

	/**
	 * 短信验证码登录
	 *
	 * @return
	 */
	@PostMapping("/loginBySms")
	public ResponseModel<String> loginBySms(@RequestBody @Validated LoginParamBySms loginParam) {

		ICodeAndData<Number> codeAndData = this.userService.loginBySms(loginParam);

		ResponseCode responseCode = codeAndData.getResponseCode();

		if (0 != ResponseCode.SUCCESS.compareTo(responseCode)) {
			return ResponseModel.<String>errorMsg("", responseCode);
		}

		Map<String, Object> map = new HashMap<>();

		map.put("id", codeAndData.getData().longValue());

		String token = jwtComponent.generateToken(map);

		return ResponseModel.<String>success(token);
	}

	/**
	 * 使用邮箱注册
	 *
	 * @param paramByMobile
	 * @return
	 */
	@PostMapping("/registerByEmail")
	@Transactional
	public ResponseModel<Number> registerByEmail(@RequestBody @Validated UserAddParam paramByEmail) {
		ICodeAndData<IdAndRecommendName> registerByEmail = this.userService.registerByEmail(paramByEmail);
		ResponseCode responseCode = registerByEmail.getResponseCode();

		if (0 != ResponseCode.SUCCESS.compareTo(responseCode)) {
			return ResponseModel.<Number>errorMsg(0, responseCode.getMsg());
		}

		String recommendedCode = paramByEmail.getRecommendedCode();

		if (null != recommendedCode) {
			
			commissionManagementService.insertRecommendUser(Integer.valueOf(recommendedCode),
					registerByEmail.getData().getRecommendName());
		}

		return ResponseModel.<Number>success(registerByEmail.getData().getId());
	}

	/**
	 * 使用手机号注册
	 *
	 * @param paramByMobile
	 * @return
	 */
	@PostMapping("/registerByPhone")
	public ResponseModel<Number> registerByPhone(@RequestBody @Validated RegisterParamByMobile paramByMobile) {
		return ResponseModel.<Number>response(this.userService.registerByPhone(paramByMobile));
	}

	/**
	 * 通过邮箱找回密码
	 *
	 * @param paramByMobile
	 * @return
	 */
	@PostMapping("/retrievePasswordByEmail")
	public ResponseModel<Number> retrievePasswordByEmail(
			@RequestBody @Validated RetrievePasswordParamByEmail paramByEmail) {
		return ResponseModel.<Number>response(this.userService.retrievePasswordByEmail(paramByEmail));
	}

	/**
	 * 通过手机号找回密码
	 * 
	 * @param paramByMobile
	 * @return
	 */
	@PostMapping("/retrievePasswordByPhone")
	public ResponseModel<Number> retrievePasswordByPhone(
			@RequestBody @Validated RetrievePasswordParamByPhone paramByPhone) {
		return ResponseModel.<Number>response(this.userService.retrievePasswordByPhone(paramByPhone));
	}

	@Resource(name = "jwtUtil")
	public void setJwtComponent(JwtTokenUtil jwtComponent) {
		this.jwtComponent = jwtComponent;
	}
}
